#include "dbms_device.h"


namespace dbms {
DbmsDevice::DbmsDevice(const std::string &dbFile)
	: Dbms<Device_t>(dbFile, "device")
{
	CreateTable("device");
}
void DbmsDevice::Save(const Device_t &dc)
{
	const std::string id = FindEqual(
		dc, [](const dbms::Device_t &t1, const dbms::Device_t &t2) -> bool {
			return t1.id() == t2.id();
		});
	if (id != "") {
		Update(id, dc);
	} else {
		Insert(dc.id(), dc);
	}
}
void DbmsDevice::Get(std::function<void(const Device_t &dc)> onGet)
{
	GetAll(onGet);
}

std::vector<Device_t> DbmsDevice::Get()
{
	std::vector<Device_t> listDc;
	GetAll([&listDc](const dbms::Device_t &dc) { listDc.push_back(dc); });
	return listDc;
}

std::string DbmsDevice::Serialize(const Device_t &t)
{
	return t.SerializeAsString();
}

Device_t DbmsDevice::Deserialize(const std::string &data)
{
	Device_t t;
	t.ParseFromString(data);
	return t;
}
} // namespace dbms